var  user = window.localStorage.getItem('user');
if(user){
    if(user === "2"){
        $(".user").html("路人甲")
    }else if(user === "1") {
        $(".user").html("管理员")
    }
}else {
    MIM.prompt('未登录', 'danger', 2000);
    setTimeout(function () {
        window.location.href = "./html/login.html";
    },1500);
}
//时间选择器
var key_time = '';
initializeTime();
function initializeTime() {
    var time = new Date();
    var year = time.getFullYear();
    var month = time.getMonth() + 1;
    var day = time.getDate();
    month = month < 10 ? "0" + month : month;
    day = day < 10 ? "0" + day : day;
    key_time = year + '-' + month + '-' + day;
    init();
}
var options = [];
timeInit();
function timeInit() {
    MIM.ajaxUtils('http://www.taoerhuo.top/curl/public/index.php/admin/index/week', {
    }, 'true', 'post', 'json', function (data) {
        if (data.code == 200) {
            var retData = data.data.list;
            $.each(retData,function (i,val) {
                options.push({
                    id:val,
                    value:val
                })
            });
            $('#timeSelect').selectInit(options);
            $('#timeSelect option:selected').html(key_time);
            $("#timeSelect").find('input').val($('#timeSelect option:selected').html());
        } else {
            MIM.prompt('失败', 'danger', 2000);
        }
    }, function (ret) {
        MIM.prompt('失败', 'danger', 2000);
    });
}
var record_one = '';
var record_two = '';
var result = '';
function init() {
    MIM.ajaxUtils('http://www.taoerhuo.top/curl/public/index.php/admin/index/handleMsg', {
        key_time: key_time
    }, 'true', 'post', 'json', function (data) {
        if (data.code == 200) {
            // MIM.prompt(data.message, 'success', 2000);
            var retData = data.data;
            var html = appendHtml(retData);
            $('.list').append(html);
            $('mim-table').find('.mim-table tr .is-leaf').css('borderRight', '1px solid #ebeef5')
        } else {
            MIM.prompt(data.message, 'danger', 2000);
        }
    }, function (ret) {
        MIM.prompt('失败', 'danger', 2000);
    });
}

$("#timeSelect").on('click','ul li',function () {
    key_time = $(this).html();
    $(".list").empty();
    init()
});



function appendHtml(ret) {
    var data = ret;
    var html = '';
    $.each(data, function (i, val) {
        var consequence = '',letConsequence = '';
        html += ' <tr><td class="is-leaf" width="100">';
        html += ' <div class="cell">'+val["week_number"]+'</div></td>';
        html += ' <td class="is-leaf" width="100">';
        html += '<div class="cell">'+val["match"]+'</div> </td>';
        html += ' <td class="is-leaf" width="110">';
        html += '<div class="cell">'+val["start_time"]+'</div> </td>';
        html += ' <td class="is-leaf" width="300">';
        html += ' <div class="cell">'+val["home_team"]+'&nbsp;<span style="color: #f56c6c;font-weight: bold">'+val["record"]+'</span>&nbsp;'+val["visiting_team"]+'</div></td>';
        html += ' <td class="is-leaf" width="300">';
        html += ' <div class="cell"> 胜：'+val["sheng"]+'  &nbsp;&nbsp; 平：'+val["ping"]+'  &nbsp;&nbsp; 负：'+val["fu"]+' </div></td>';
        html += '<td class="is-leaf" width="150">';
        if(val["record"] == "VS"){
            html += '<div class="cell" style="color: #909399;font-weight: bold">未开赛</div></td>';
        }else {
            if(Number(val["record_one"]) > Number(val["record_two"])){
                consequence = "胜"
            }else if(Number(val["record_one"]) == Number(val["record_two"])){
                consequence = "平"
            }else if(Number(val["record_one"]) < Number(val["record_two"])){
                consequence = "负"
            }
            if((Number(val["record_one"])+ Number(val["let_ball"])) > Number(val["record_two"])){
                letConsequence = "让胜"
            }else if((Number(val["record_one"])+ Number(val["let_ball"])) == Number(val["record_two"])){
                letConsequence = "让平"
            }else if((Number(val["record_one"])+ Number(val["let_ball"])) < Number(val["record_two"])){
                letConsequence = "让负"
            }
            html += '<div class="cell" style="color: #f56c6c;font-weight: bold">'+val["record"]+'&nbsp;&nbsp;'+consequence+','+letConsequence+'('+val["let_ball"]+')</div></td>';
            var  arr = val["forecast"].split(',');
            if(val["forecast"]){
                for(var j=0;j<arr.length;j++){
                    if(arr[j].indexOf(consequence) == -1 && arr[j].indexOf(letConsequence) == -1){
                        val["red"] = '黑'
                    }else {
                        val["red"] = '红'
                    }
                }
            }

        }
        html += ' <td class="is-leaf" width="150">';
        if(val["forecast"]){
            html += ' <div class="cell" style="font-size: 14px;color: #f56c6c;">'+val["forecast"]+'</div></td>';
        }else {
            html += ' <div class="cell" style="color: #909399">未预测</div></td>';
        }
        html += ' <td class="is-leaf" width="100">';
        if(val["red"]){
            if(val["red"] == "红"){
                html += ' <div class="cell" style="font-size: 16px;color: #f56c6c;font-weight: bold">'+val["red"]+'</div></td>';
            }else {
                html += ' <div class="cell" style="font-size: 14px;color: #909399;">'+val["red"]+'</div></td>';
            }
        }else {
            html += '<div class="cell" style="color: #f56c6c;">祝红</div></td>';
        }
        html += '<td class="is-leaf">';
        html += '<div class="cell" style="text-align: center">';
        if(user !== "2"){
            html += '<mim-button type="primary" size="medium" onclick="goTo(this)" indexId='+val["id"]+'>点击预测</mim-button>';
        }else {
            console.log(user);
            html += '<span>提前半小时公布</span>';
        }
        html += '</div></td></tr>';
    });
    return html;
}

function goTo(that) {
    var id = $(that).attr('indexId');
    window.location.href = "./html/compile.html?id="+id
}




